import hmac
import json
import base64
import datetime
import urllib
from hashlib import sha256
from collections import OrderedDict
import urllib.request, urllib.error, urllib.parse
import pymysql
import requests


class Qingyun:
    def __init__(self):
        self.access_key_id = 'NVZXMHPSPKAKKYOTADQZ'
        self.secret_access_key = '38SBm8EVk6N138NG9sriqp7h2VOEqXpuKnx5Dyag'
        self.time_stamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
        self.url = "http://api.qingcloud.com/iaas/"
        self.url_path = '/iaas/'
        self.methods = 'GET'
        self.zone = 'pek3a'

    def postResust(self, iassurl):
        req = urllib.request.Request(iassurl)
        result = urllib.request.urlopen(req)
        response = json.loads(result.read().decode())
        print(response)

    def ecs_Check(self, resource):
        od = OrderedDict()
        od['access_key_id'] = self.access_key_id
        od['action'] = "GetMonitor"
        od['resource'] = resource
        od['meters.1'] = "cpu"
        od['meters.2'] = "memory"
        od['meters.3'] = "disk-us-os"
        od['start_time'] = "2022-12-04T09:00:22Z"  # 时间目前写死了
        od['end_time'] = "2022-12-04T09:05:22Z"
        od['signature_method'] = "HmacSHA256"
        od['time_stamp'] = self.time_stamp
        od['step'] = '5m'
        od['signature_version'] = 1
        od['zone'] = self.zone
        self.time_stamp

        od = self.sort_value(od)
        # print(od)
        data = urllib.parse.urlencode(od)
        # print(data)
        string_to_sign = self.methods + "\n" + self.url_path + "\n" + data
        h = hmac.new(self.secret_access_key.encode(), digestmod=sha256)
        h.update(string_to_sign.encode())
        sign = base64.b64encode(h.digest()).strip()
        signature = urllib.parse.quote_plus(sign)
        # print("signature是:"+signature)
        iaasUrl = self.url + "?" + data + "&signature=" + signature
        # print(iaasUrl)
        self.postResust(iaasUrl)

    def ecs_Info(self):
        od = OrderedDict()
        od['access_key_id'] = self.access_key_id
        od['action'] = "DescribeInstances"
        od['signature_method'] = "HmacSHA256"
        # od['image_id.1'] = "img-vbp7ftes"
        od['signature_version'] = 1
        od['time_stamp'] = self.time_stamp
        od['version'] = 1
        od['zone'] = self.zone
        od['status.1'] = "running"
        od['status.2'] = "stopped"
        od['limit'] = "100"
        od['offset'] = "0"

        od = self.sort_value(od)
        # print(od)
        data = urllib.parse.urlencode(od)
        # print(data)
        string_to_sign = self.methods + "\n" + self.url_path + "\n" + data
        h = hmac.new(self.secret_access_key.encode(), digestmod=sha256)
        h.update(string_to_sign.encode())
        sign = base64.b64encode(h.digest()).strip()
        signature = urllib.parse.quote_plus(sign)
        # print("signature是:"+signature)
        iaasUrl = self.url + "?" + data + "&signature=" + signature
        # print(iaasUrl)
        self.postResust(iaasUrl)

    def ecs_Price(self,resource):
        od = OrderedDict()
        od['access_key_id'] = self.access_key_id
        od['action'] = "GetChargeSums"
        od['signature_method'] = "HmacSHA256"
        # od['image_id.1'] = "img-vbp7ftes"
        od['signature_version'] = 1
        od['time_stamp'] = self.time_stamp
        od['version'] = 1
        od['zone'] = self.zone
        od['resource'] = resource
        od['duration'] = "weekly"
        od['limit'] = "2"

        od = self.sort_value(od)
        # print(od)
        data = urllib.parse.urlencode(od)
        # print(data)
        string_to_sign = self.methods + "\n" + self.url_path + "\n" + data
        h = hmac.new(self.secret_access_key.encode(), digestmod=sha256)
        h.update(string_to_sign.encode())
        sign = base64.b64encode(h.digest()).strip()
        signature = urllib.parse.quote_plus(sign)
        # print("signature是:"+signature)
        iaasUrl = self.url + "?" + data + "&signature=" + signature
        # print(iaasUrl)
        self.postResust(iaasUrl)
    def sort_value(self, old_dict):
        items = sorted(old_dict.items())
        new_dict = OrderedDict()
        for item in items:
            new_dict[item[0]] = old_dict[item[0]]
        return new_dict

if __name__ == '__main__':

    resource = 'i-vgfe6tl5'
    x = Qingyun()
    # x.ecs_Check(resource)
    # x.ecs_Info()
    x.ecs_Price(resource)
